﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace HYWJ.jxgl
{
    /// <summary>
    /// 态势管理
    /// </summary>
    /// <remarks></remarks>
    public partial class TSGLForm : Office2007Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TSGLForm"/> class.
        /// </summary>
        /// <remarks></remarks>
        public TSGLForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选中节点下创建子节点
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != null && textBox1.Text != "")
            {
                if (treeView1.SelectedNode != null)
                {
                    ////选中的节点下创建
                    treeView1.SelectedNode.Nodes.Add(textBox1.Text);
                }
                else
                {
                    treeView1.Nodes[0].Nodes.Add(textBox1.Text);
                }
            }
            else
            {
                MessageBox.Show("想定名不能为空");
                return;
            }
        }

        /// <summary>
        /// 删除选中节点
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button3_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                if (MessageBox.Show("确定要删除选中的想定方案，如果删除该想定下的补充想定也会同时删除！", "注意", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    treeView1.SelectedNode.Remove();
                }
                else
                {
                    return;
                }
            }

        }

        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("修改以保存！");
            if (treeView1.SelectedNode.Text == "想定一")
            {
                save(Application.StartupPath + @"\media\yuyin\sound1.txt");
            }
            else if (treeView1.SelectedNode.Text == "补充想定-1")
            {
                save(Application.StartupPath + @"\media\yuyin\sound2.txt");
            }
        }
        
        /// <summary>
        /// 选中树节点改变
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.TreeViewEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
            if (treeView1.SelectedNode.Text.ToString() == "想定一")
            {
                open(Application.StartupPath+@"\media\yuyin\sound1.txt");
            }
            else if (treeView1.SelectedNode.Text.ToString() == "补充想定-1")
            {
                open(Application.StartupPath + @"\media\yuyin\sound2.txt");
            }
            else
            {
                richTextBox1.Text = "";
            }
            
        }

        /// <summary>
        /// Opens the specified file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <remarks></remarks>
        private void open(string file)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(file, System.Text.Encoding.Default);
            string content = reader.ReadToEnd();
            richTextBox1.Text = content;
            reader.Close();
        }

        /// <summary>
        /// Saves the specified file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <remarks></remarks>
        private void save(string file)
        {
            System.IO.StreamWriter writer = new System.IO.StreamWriter(file, false, System.Text.Encoding.Default);
            ////string content = writer.Write(richTextBox1.Text);
            writer.Write(richTextBox1.Text);
            writer.Flush();
            writer.Close();
        }
    }
}
